/*
 * @Author: qin
 * @Date:   2018-08-28
 * +----------------------------------------------------------------------
 * | admin [ 后台管理系统公共配置信息 ]
 * |
 * +----------------------------------------------------------------------
 */
layui.define(['configure', 'selectFun', 'configureCom', 'moduleApi', 'table', 'bluebird', 'sessionFun'], function (exports) {
    var configure = layui.configure;
    var configureCom = layui.configureCom;
    var table = layui.table;
    var selectFun = layui.selectFun;
    var sessionFun = layui.sessionFun;
    //回传成功的回调
    window.loadOver = function () {
        top.layer.closeAll();
        // layer.msg('回传成功');
        configureCom.tableReload(true);
    };
    var reviewUnit = {
        //加载表格
        tabReload: function (type) {
            var toolWidth = 220;
            var cols = [[ //表头
                {
                    title: '受理编号', templet: function (d) {
                        return d.acceptCode + '</br>' +
                            '<span>受理时间：' + configureCom.dateToStrAll(d.acceptTime) + '</span>';
                    }
                },
                {
                    field: 'singleProjName', title: '单位工程名称', templet: function (d) {
                        var html = configureCom.getMSName(d) +
                            '</br>' +
                            '<span>编号:' + d.singleProjCode + '</span>';
                        return html;
                    }
                },
                {
                    title: '建设单位', templet: function (d) {
                        return d.jsdwName + '</br>' +
                            '<span>区划：' + selectFun.getAreaByCode(d.country) + '</span>';
                    }
                }
            ]];
            if (type === 'dj') {
                cols[0].push({
                    title: '初审确认时间', templet: function (d) {
                        return configureCom.dateToStrAll(d.chushenFinishTime);
                    }
                });
            }
            if (type === 'qr') {
                toolWidth = 270;
                cols[0].push({
                    title: '登记时间', width: 170, templet: function (d) {
                        return configureCom.dateToStrAll(d.resultRegisterTime);
                    }
                });
            }
            if (type === 'ba') {
                toolWidth = 380;
                cols[0].push({
                    title: '确认时间', width: 170, templet: function (d) {
                        return configureCom.dateToStrAll(d.resultConfirmTime);
                    }
                });
                cols[0].push({
                    title: '状态', templet: function (d) {
                        var isBa = (d.resultRegisterStep === '1030' ? '未备案' : '已备案') + '</br>';
                        var baTime = '<span>备案时间:' + configureCom.dateToStrAll(d.resultRecordTime) + '</span>';
                        return isBa + (d.resultRegisterStep === '1040' ? baTime : '');
                    }
                });
            }
            cols[0].push({title: '操作', width: toolWidth, align: 'center', toolbar: '#barDemo'});
            var url = configure.SEAL_SEAL_LIST_HTTP;
            var data = {
                listType: type //dj,qr,ba
            };
            configureCom.tableRender(url, data, cols).then(function (res) {
                if (!configureCom.GetQueryString('addPadding')) {
                    var count = res.count;
                    var name = '';
                    if (type === 'dj') {
                        name = 'register';
                    } else if (type === 'qr') {
                        name = 'confirm';
                    } else if (type === 'ba') {
                        name = 'recordBa';
                    }
                    if (name) {
                        parent.resetTab(name, count);
                    }
                }
            });
        },
        tool: function (obj) {
            var data = obj.data;
            var ttSgProjApplyItemId = data.ttSgProjApplyItemId;
            var singleProjId = data.singleProjId || data.singleProjectId;
            sessionFun.set(configure.SESSION_SIGN_DATA, data);
            if (obj.event === 'detail') {//查看详情
                url = configure.ALL_COMMON_DETAIL + '?singleProjectId=' + singleProjId;
                var title = '查看';
                configureCom.klmyPop([url, title]);
            } else if (obj.event === 'backManage') {//回传综合报告书管理
                var upmUrl = configure.MANAGEMENT_BACK_HTML + '?singleProjId=' + singleProjId + '&bookType=1';// bookType:0合格书 \ 1报告书;
                var upmTitle = '回传综合报告书管理';
                configureCom.klmyPop([upmUrl, upmTitle, 0, '500*240']);
            } else if (obj.event === 'backManageH') {//回传合格书管理
                var upmUrlh = configure.MANAGEMENT_BACK_HTML + '?singleProjId=' + singleProjId + '&bookType=0';// bookType:0合格书 \ 1报告书;
                var upmTitleh = '回传合格书管理';
                configureCom.klmyPop([upmUrlh, upmTitleh, 0, '500*240']);
            }
            else if (obj.event === 'dj') {//审查成果登记
                top.layer.confirm('请仔细核对建设单位上报的信息。', {title: '确认信息'}, function (index) {
                    reviewUnit.layerDj(data, 'edit');
                    top.layer.close(index);
                });

            } else if (obj.event === 'goBack') {//已登记退回
                top.layer.confirm('确认退回？', {title: '确认信息'}, function (index) {
                    var backUrl = configure.SEALPUT_GOBACK_HTTP;
                    var backData = {
                        ttSgProjApplyItemId: ttSgProjApplyItemId
                    };
                    configureCom.putDataAjax(backUrl, backData).then(function (data) {
                        if (data.code === 1) {
                            top.layer.msg('退回成功！');
                            configureCom.tableReload(false);
                            parent.refreshIframe();
                        }
                    });
                    top.layer.close(index);
                });
            } else if (obj.event === 'agree') {//确认
                top.layer.confirm('请仔细核对建设单位上报的信息。若填报的信息有误，请及时退回到【审查结果登记】中修改。', {title: '确认信息'}, function (index) {
                    reviewUnit.layerDj(data, 'readOnly');
                    top.layer.close(index);
                });
            } else if (obj.event === 'ba') {//备案
                top.layer.confirm('确定提交该审查结果备案吗? 提交审查结果成功后，即审图环节结束，将不允许修改合格书中相关信息。' +
                    '注意：备案无需审核提交即可通过。', {title: '确认信息'}, function (index) {
                    var bUrl = configure.SEALPUT_PUT_ON_RECORD_HTTP;
                    var bData = {
                        ttSgProjApplyItemId: ttSgProjApplyItemId
                    };
                    configureCom.putDataAjax(bUrl, bData).then(function (data) {
                        if (data.code === 1) {
                            top.layer.msg('备案成功！');
                            parent.refreshIframe();
                        }
                    });
                    top.layer.close(index);
                });
            } else if (obj.event === 'downBGS') {//下载报告书
                var bookUrl1 = configure.EXPORT_UNSEAL_REPORT_HTTP + '?ttSgProjApplyItemId=' + ttSgProjApplyItemId;
                location.href = bookUrl1;
            } else if (obj.event === 'downHGS') {//下载合格书
                var bookUrl = configure.EXPORT_UNSEAL_QUALIFY_HTTP + '?ttSgProjApplyItemId=' + ttSgProjApplyItemId;
                location.href = bookUrl;
            } else if (obj.event === 'hgsSignBtn') {//合格书pdf生成+签章保存
                configureCom.getAjax(configure.EXPORT_HEGESHU_SIGN_PDF_HTTP, {ttSgProjApplyItemId: ttSgProjApplyItemId}).then(function (res) {
                    var signData = {bookType: 0, fileName: res.msg, ttSgProjApplyItemId: ttSgProjApplyItemId};
                    sessionFun.set(configure.SESSION_SIGN_DATA, signData);
                    top.layer.open({
                        type: 2,
                        area: configureCom.Psize1,
                        content: configure.EXPORT_HEGESHU_SIGN_PDF_HTML,
                        btn: ['保存签章'],
                        yes: function (index, layero) {
                            var iframeWin = top.window.frames['layui-layer-iframe' + index]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                            iframeWin.signSave().then(function (res) {
                                if (res.code == 1) {
                                    top.layer.msg(res.msg);
                                    top.layer.close(index);
                                    configureCom.tableReload();
                                } else {
                                    top.layer.alert(res.msg);
                                }
                            });
                        }
                    });
                });
            } else if(obj.event ==='hasSignBtn'){//已签章预览
                var signData = {bookType: bookType, fileName: (bookType==0?data.hgsFileName:data.bgsFileName), ttSgProjApplyItemId: ttSgProjApplyItemId};
                sessionFun.set(configure.SESSION_SIGN_DATA, signData);
                top.layer.open({
                    type: 2,
                    area: configureCom.Psize1,
                    content: configure.EXPORT_HEGESHU_SIGN_PDF_HTML,
                    btn: ['重新签章'],
                    yes: function (hasIndex, hasLayero) {
                        var url = bookType==0?configure.EXPORT_HEGESHU_SIGN_PDF_HTTP:configure.EXPORT_BAOGAOSHU_SIGN_PDF_HTTP;
                        configureCom.getAjax(url, {ttSgProjApplyItemId: ttSgProjApplyItemId}).then(function (res) {
                            var signData = {bookType: bookType, fileName: res.msg, ttSgProjApplyItemId: ttSgProjApplyItemId};
                            sessionFun.set(configure.SESSION_SIGN_DATA, signData);
                            top.layer.open({
                                type: 2,
                                area: configureCom.Psize1,
                                content: configure.EXPORT_HEGESHU_SIGN_PDF_HTML,
                                btn: ['保存签章'],
                                yes: function (index, layero) {
                                    var iframeWin = top.window.frames['layui-layer-iframe' + index]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                                    iframeWin.signSave().then(function (res) {
                                        if (res.code == 1) {
                                            top.layer.msg(res.msg);
                                            top.layer.close(index);
                                            configureCom.tableReload();
                                        } else {
                                            top.layer.alert(res.msg);
                                        }
                                    });
                                }
                            });
                        });
                        top.layer.close(hasIndex);
                    }
                });
            }else if (obj.event === 'downSignBtn') {//下载合格书报告书
                var fileName = bookType==0?data.hgsFileName:data.bgsFileName;
                location.href=configure.DRAWING_DOWNLOAD+'?fileName='+fileName;
            } else if (obj.event === 'bgsSignBtn') {//报告书pdf生成+签章保存
                configureCom.getAjax(configure.EXPORT_BAOGAOSHU_SIGN_PDF_HTTP, {ttSgProjApplyItemId: ttSgProjApplyItemId}).then(function (res) {
                    var signData = {bookType: bookType, fileName: res.msg, ttSgProjApplyItemId: ttSgProjApplyItemId};
                    sessionFun.set(configure.SESSION_SIGN_DATA, signData);
                    top.layer.open({
                        type: 2,
                        area: configureCom.Psize1,
                        content: configure.EXPORT_HEGESHU_SIGN_PDF_HTML,
                        btn: ['保存签章'],
                        yes: function (index, layero) {
                            var iframeWin = top.window.frames['layui-layer-iframe' + index]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                            iframeWin.signSave().then(function (res) {
                                if (res.code == 1) {
                                    top.layer.msg(res.msg);
                                    top.layer.close(index);
                                    configureCom.tableReload();
                                } else {
                                    top.layer.alert(res.msg);
                                }
                            });
                        }
                    });
                });
            }
        },
        //打开登记弹窗
        layerDj: function (data, type) {
            var btn = 1;
            var title = '登记审查结果';//成果登记
            if (type === 'readOnly' || type === 'detail') {
                title = '确认审查结果';//成果确认
                btn = '确认';
            }
            var registerResultData = $.extend(true, {type: type}, data);
            localStorage.registerResultData = JSON.stringify(registerResultData);//登记勘察成果数据
            var url = configure.KCDJ_REGINST_RESUTL_HTML;
            configureCom.klmyPop([url, title, btn], function (index, layero) {
                    var iframeWin = top.window.frames['layui-layer-iframe' + index]; //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                    iframeWin.parWin = window;
                    if (type === 'edit') {
                        configureCom.loadON();
                        iframeWin.djInfo(true);
                    } else {
                        //确认
                        var agrUrl = configure.SEALPUT_CONFIRM_HTTP;
                        var agrData = {
                            ttSgProjApplyItemId: data.ttSgProjApplyItemId
                        };
                        configureCom.putDataAjax(agrUrl, agrData).then(function (data) {
                            if (data.code === 1) {
                                top.layer.close(index);
                                top.layer.msg('确认成功！');
                                configureCom.tableReload();
                                parent.refreshIframe();
                            }
                        });
                    }
                }
            );
        }
    };
    exports('reviewUnit', reviewUnit);
});